#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution
{
public:
    string frequencySort(string s)
    {
        // 统计字符出现的次数
        unordered_map<char, int> m;
        for (auto c : s)
        {
            m[c]++;
        }
        // 根据出现次数排序
        vector<pair<char, int>> v;
        for (auto it = m.begin(); it != m.end(); it++)
        {
            v.push_back(make_pair(it->first, it->second));
        }
        sort(v.begin(), v.end(), [](pair<char, int> a, pair<char, int> b)
             { return a.second > b.second; });
        // 根据出现次数排序的字符重新组成字符串
        string res = "";
        for (auto p : v)
        {
            res += string(p.second, p.first);
        }
        return res;
    }
};
int main()
{
    Solution s;
    cout << s.frequencySort("tree") << endl;
    system("pause");
    return 0;
}
